﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
public class LevelJump : MonoBehaviour
{
    public GameLevelItem gameLevelItem;
    public List<TextButton> tbList = new List<TextButton>();
    public string targetStr = "jump";
    private List<string> loveStrList = new List<string>();
    private bool win = false;
    // Start is called before the first frame update
    void Start()
    {
        InitTbList();
        loveStrList.Add("l");
        loveStrList.Add("o");
        loveStrList.Add("v");
        loveStrList.Add("e");
    }

    private void InitTbList()
    {
        int count = tbList.Count;
        for (int i = 0; i < count; i++)
        {
            int tempIndex = i;
            tbList[i].Init(string.Empty, () =>
            {
                if (win) return;
                string curStr = tbList[tempIndex].text.text;
                curStr = GetNextLetter(curStr);
                tbList[tempIndex].text.text = curStr;
                JudgeResult();
            });
        }
    }

    private string GetNextLetter(string curLetter)
    {
        string next = string.Empty;
        switch (curLetter)
        {
            case "p":
                next = "u";
                break;
            case "u":
                next = "j";
                break;
            case "j":
                next = "m";
                break;
            default:
                next = "p";
                break;
        }
        return next;
    }

    private void JudgeResult()
    {
        string tempStr = string.Empty;
        int count = tbList.Count;
        for (int i = 0; i < count; i++)
        {
            tempStr += tbList[i].text.text.Trim();
        }
        if (tempStr.Equals(targetStr))
        {
            win = true;
            SoundManager.Instance.PlayAudio(SoundType.CountDown.ToString(), false);
            for (int i = 0; i < count; i++)
            {
                tbList[i].button.image.DOFade(0, 0.5f);
            }
            Invoke("TextAni", 0.5f);
        }

    }
    int textAniIndex = 0;
    private void TextAni()
    {
        if (textAniIndex < tbList.Count)
        {
            SoundManager.Instance.PlayAudio(SoundType.Jump.ToString(), false);
            tbList[textAniIndex].text.text = loveStrList[textAniIndex];
            textAniIndex++;
            Invoke("TextAni", 0.5f);
        }
        else
        {
            Invoke("TriggerWin", 0.5f);
        }
    }

    private void TriggerWin()
    {
        gameLevelItem.TriggerResult(true);
    }
}
